package cn.yanglq.Rss.web.controller.userinfo;

import cn.yanglq.Rss.web.common.vo.ApiResult;
import cn.yanglq.Rss.web.controller.BaseController;
import cn.yanglq.Rss.web.model.User;
import cn.yanglq.Rss.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by yanglq on 2018/1/17.
 */
@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping("/userinfopage")
    public ModelAndView userinfopage() {
        String Loginid = findCurrentLoginid();
        User user = userService.selectUserByUserId(Loginid);

        ModelAndView mv = new ModelAndView();
        mv.addObject("user", user);
        mv.setViewName("/userinfo/edituserinfo");
        return mv;
    }

    @RequestMapping("/edituserinfo")
    @ResponseBody
    public ApiResult edituserinfo(@RequestParam(value = "username", required = true) String username,
                                  @RequestParam(value = "email", required = true) String email,
                                  @RequestParam(value = "mobilenumber", required = true) String mobilenumber) {
        ApiResult result = new ApiResult();
        User user = new User();
        String userid = findCurrentLoginid();
        user.setMobilenumber(mobilenumber);
        user.setEmail(email);
        user.setUsername(username);
        userService.updateInfo(userid, user);
        result.setSuccess(true);
        result.setErrorcode(0);
        result.setMessage("修改成功！");
        return result; //校验通过返回成功
    }

}
